package com.douglas.managesystem.service.student.impl;

import com.douglas.managesystem.common.RoleTypeEnum;
import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.dao.student.StudentMapper;
import com.douglas.managesystem.dao.student.TeacherMapper;
import com.douglas.managesystem.entity.studentpojo.StuRole;
import com.douglas.managesystem.entity.studentpojo.Teacher;
import com.douglas.managesystem.service.student.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 教师业务实现类
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-17 12:45
 **/
@SuppressWarnings("all")
@Service
public class TeacherServiceImpl implements TeacherService {


    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 1.分页查询所有教师信息
     * @param params
     * @return
     */
    @Override
    public PageInfo<Teacher> findAllTeacherByPage(SearchEntity params) {

        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Teacher> list = teacherMapper.queryListByPage(params.getEntity());
        PageInfo<Teacher> page = new PageInfo<Teacher>(list);
        return page;
    }


    /**
     * 2.根据id查询教师信息
     * @param id
     * @return
     */
    @Override
    public Teacher findTeacherById(String id) {
        return teacherMapper.queryTeacherById(id);
    }

    /**
     * 3.新增教师信息
     * @param teacher
     */
    @Override
    public void addTeacher(Teacher teacher) {
        //1.验证老师是否存在，若存在不执行新增处理
        Teacher teach = teacherMapper.queryTeacherByTeacherId(teacher.getTeacherId());
        if(teach != null){
            return;
        }
        // 1.创建教师角色中间表对象 新增当前教师角色信息到中间表
        StuRole stuRole = new StuRole();
        // 1.1 教师工号id
        stuRole.setStuNumber(teacher.getTeacherId());
        // 1.1 教师角色 1
        stuRole.setRoleType(RoleTypeEnum.TEACHER.getCode());
        // 2.用户密码加密的盐
        ByteSource salt = ByteSource.Util.bytes(teacher.getTeacherId() + "123456");
        // 3.加盐后的密码
        SimpleHash saltPassword = new SimpleHash("MD5", "123456", salt, 1024);
        // 4.设置初始密码
        teacher.setPassword(saltPassword.toString());
        // 5.新增信息
        teacherMapper.insert(teacher);
        // 6.新增当前教师的角色信息
        studentMapper.insertStuRole(stuRole);
    }

    /**
     * 4.更新教师信息
     * @param teacher
     */
    @Override
    public void editTeacher(Teacher teacher) {
        // 1.直接更新
        teacherMapper.update(teacher);
    }

    /**
     * 5.根据id删除教师信息
     * @param id
     */
    @Override
    public void deleteTeacherById(String id) {
        teacherMapper.delete(id);
    }

    /**
     * 6.根据teacherId查询教师对象
     * @param teacherId
     * @return
     */
    @Override
    public Teacher queryTeacherByTeacherId(String teacherId) {
        return teacherMapper.queryTeacherByTeacherId(teacherId);
    }

    /**
     * 7.查询所有教师id
     * @return
     */
    @Override
    public List<String> queryAllTeacherList() {
        return teacherMapper.queryAllTeacherList();
    }
}
